package cn.ylz.ylmall.account.controller;

import cn.ylz.ylmall.account.pojo.param.RoleAddNewParams;
import cn.ylz.ylmall.account.pojo.param.RoleAddParams;
import cn.ylz.ylmall.account.pojo.param.RoleUpdateParams;
import cn.ylz.ylmall.account.pojo.vo.*;
import cn.ylz.ylmall.account.service.IRolerService;
import cn.ylz.ylmall.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/v1/users/role")
@Api(tags = "1.3. 账号管理-角色管理")
@Validated
public class RoleController {
    @Autowired
    IRolerService rolerService;

    @PostMapping("add-new")
    @ApiOperation("新增角色")
    @PreAuthorize("hasAuthority('/account/role/add-new')")
    @ApiOperationSupport(order = 1)
    public JsonResult addNew(@Valid RoleAddNewParams roleAddNewParams){
        rolerService.addnew(roleAddNewParams);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("删除角色")
    @PreAuthorize("hasAuthority('/account/role/delete')")
    @ApiOperationSupport(order = 3)
    public JsonResult delete(@PathVariable @Range(min = 1, message = "请提交有效的角色ID值！") Long id){
        rolerService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改角色")
    @PreAuthorize("hasAuthority('/account/role/update')")
    @ApiOperationSupport(order = 4)
    public JsonResult update(@PathVariable @Range(min = 1, message = "请提交有效的权限ID值！") Long id, @Valid RoleUpdateParams param){
        rolerService.update(id,param);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询角色")
    @PreAuthorize("hasAuthority('/account/role/read')")
    @ApiOperationSupport(order = 10)
    public JsonResult getStandardById(@PathVariable @Range(min = 1, message = "请提交有效的角色ID值！") Long id){
        RoleStandardVO standardVO = rolerService.getStandardById(id);
        return JsonResult.ok(standardVO);
    }

    @GetMapping("")
    @ApiOperation("查询角色列表")
    @PreAuthorize("hasAuthority('/account/role/read')")
    @ApiOperationSupport(order = 20)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", dataType = "long")
    })
    public JsonResult list(Integer page){
        Integer pageNum = page == null ? 1 : page;
        PageData<RoleListVO> list = rolerService.list(pageNum);
        return JsonResult.ok(list);
    }

    @PostMapping("/{roleId:[0-9]+}/addPermission")
    @ApiOperation("给角色新增权限")
    @PreAuthorize("hasAuthority('/account/role/add')")
    @ApiOperationSupport(order = 2)
    public JsonResult add(@PathVariable @Range(min = 1, message = "请提交有效的角色ID值！") Long roleId, @Valid RoleAddParams param){
        rolerService.add(roleId,param);
        return JsonResult.ok();
    }

    @GetMapping("/{roleId:[0-9]+}/permission")
    @ApiOperation("查询角色下的所有权限")
    @PreAuthorize("hasAuthority('/account/permission/read')")
    @ApiOperationSupport(order = 30)
    public JsonResult listPermissions(@PathVariable @Range(min = 1, message = "请提交有效的角色ID值！") Long roleId){
        List<UserPermissionVO> list = rolerService.PermissionsById(roleId);
        return JsonResult.ok(list);
    }

}
